package cm.com.blackboard.DataModule4B2.customizeConfigUtils;

import java.beans.IntrospectionException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.jdom.JDOMException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import blackboard.data.BbObject;
import cn.com.blackboard.DataModule4B2.model.BaseModel;
import cn.com.blackboard.DataModule4B2.util.ExtentionModel;

public class ExtentionAttributeUtil {

	private static final Logger logger = LoggerFactory.getLogger(ExtentionAttributeUtil.class);
	
	private static String profilepath=ExtentionAttributeUtil.class.getClassLoader().getResource("/Extention.xml").getPath();
	//private static String profilepath = System.getProperty("user.dir")+"/target/classes/Extention.xml";
    private static List<ExtentionModel> extentionList = new ArrayList<ExtentionModel>(); 
    static { 
    	logger.info("Extention Attribute configu file load from "+profilepath);
        try {
        	extentionList = BaseXMLUtil.parseXML(profilepath, ExtentionModel.class);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    } 
    
   
    
    public static boolean setAndPersistExtentionList(List<ExtentionModel> emList){
		extentionList = emList;
		try {
			BaseXMLUtil.renderXML(profilepath, ExtentionModel.class, extentionList);
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			return false;
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			return false;
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			return false;
		} catch (IntrospectionException e) {
			e.printStackTrace();
			return false;
		}
	}
    
	public static List<ExtentionModel> loadExtention(BbObject bbobj, BaseModel bm){
		List<ExtentionModel> tarList = new ArrayList<ExtentionModel>();
		String modelClsName = bm.getClass().getName();
		String bbClsName = bbobj.getClass().getName();
		for(ExtentionModel em:extentionList ){
			if(em.getModelClsName().equals(modelClsName)&&em.getBbClsName().equals(bbClsName))
				tarList.add(em);
		}
		return tarList;
	}
	
	public static List<ExtentionModel> getExtentionList(){
		return extentionList;
	}	
	
	
	public static boolean hasExtention(BbObject bbobj, BaseModel bm){
		return loadExtention(bbobj, bm).size()!=0;
	}
	
	public static void setExtentionAttribute(BbObject bbobj, BaseModel model){
		logger.info("start to set extention attributes");
		Class bbobjClazz = bbobj.getClass();
		Class modelClazz = model.getClass();
		Method[] setMethods = bbobjClazz.getDeclaredMethods(); 
		Method[] getMethods = modelClazz.getDeclaredMethods();
		
		String classSimpleName = modelClazz.getSimpleName();
		
		List<ExtentionModel> emList = loadExtention(bbobj, model);
		
		for(ExtentionModel em:emList){
			String objAttribute = em.getBbAttribute();
			String bbobjAttribute = em.getModelAttribute();
			
			String attribute = null;
			String getMethodName = "get" + objAttribute;
			for(int i = 0; i < getMethods.length;i++){
				if (getMethods[i].getName().equalsIgnoreCase(getMethodName)) 
					getMethodName = getMethods[i].getName();
			}
			Method getMethod;
			try {
				getMethod = modelClazz.getMethod(getMethodName);
				attribute = (String) getMethod.invoke(model);
			} catch (Exception e) {
				logger.info("error in ExtentionAttributeUtil get "+objAttribute+" from "+modelClazz.getName());
				e.printStackTrace();
			} 
			
			String setMethodName = "set" + bbobjAttribute;
			for (int j = 0; j < setMethods.length; j++) {
				
	            if (setMethods[j].getName().equalsIgnoreCase(setMethodName)) 
	                setMethodName = setMethods[j].getName(); 
	        }
			try {
				Method setMethod = bbobjClazz.getMethod(setMethodName, String.class);
				setMethod.invoke(bbobj, attribute);
			} catch (Exception e) {

				logger.info("error in ExtentionAttributeUtil set "+bbobjAttribute+" from "+bbobjClazz.getName());
				e.printStackTrace();
			} 
			
		}
	
	}
}
